package com.atguigu.demo.customer.controller;

import com.atguigu.demo.customer.bean.Customer;
import com.atguigu.demo.customer.service.CustomerService;
import com.atguigu.demo.customer.service.impl.CustomerServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController  //@Controller
public class CustomerController {

    @Autowired
    CustomerService customerService ;

    //1   键值对参数     场景：多个条件 的查询  读
    @RequestMapping("/customer")
    public List<Customer> sayHello(@RequestParam(value = "name",required = false) String name ,@RequestParam(value = "age",required = false) Integer age){
        Customer customerCondition = new Customer();
        customerCondition.setName(name);
        customerCondition.setAge(age);
       // List<Customer> customerList = customerService.getCustomerList(customerCondition);
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<Customer>().like(name != null, "name", name).eq(age != null, "age", age);
        List<Customer> customerList = customerService.list(queryWrapper);

        return customerList;
    }

    //2   路径参数      场景： 唯一性主键     读
    @RequestMapping("/customer/{id}")
    public  String getCustomerById(@PathVariable("id") Integer id ){
      //  Customer customer = customerService.getCustomerById(id);
        Customer customer = customerService.getById(id);
        return  " customer:   --123"+customer;
    }




    //3   写    请求体参数  requestbody  requestEntity  payload
   // @RequestMapping(value = "/customer",method = RequestMethod.POST)
    @PostMapping("/customer")
    public String saveCustomer(@RequestBody Customer customer){

     //   customerService.saveCustomer(customer);
        customerService.saveOrUpdate(customer);
        return  "customer : "+customer;

    }


}
